import {
    Categories
} from "../../modeal/categories.js"
const categories = new Categories()

import {
    Cart
} from "../../modeal/cart.js"

Page({
    data: {
        shop: null,
        orderType: null,
        lock: false,
        current: 1,
        scrollTopId: null,
        categories: [],
        subsItemHieghtArr: [],
        carts: [],
        currentRoot: {},
        showCart: false,
        cartScroll: false

    },
    onLoad() {
        this._getShop()
        this._getOrderType()
        this._initCategories()
    },

    changeCurrentRoots(e) {
        const current = e.currentTarget.dataset.id || e
        this.setData({
            current
        })
    },

    // 滑动右边向左联动
    subsScroll(e) {
        const scrollTop = e.detail.scrollTop
        // 查看当前scrollTop在arr中位置
        const current = categories.findCurrentRootsId(scrollTop, this.data.subsItemHieghtArr, (res) => {
            let id = this.data.categories[res].id
            this.setData({
                current: id
            })
        })
    },
    add(e) {
        const gid = e.currentTarget.dataset.gid
        this._changeCart('add', gid)
    },

    minus(e) {
        const gid = e.currentTarget.dataset.gid
        this._changeCart('minus', gid)
    },

    showCart() {
        this.setData({
            showCart: true
        })
    },

    async _changeCart(type, gid) {
        const shopId = this.data.shop.id
        const orderType = this.data.orderType
        if (this._isLocked()) {
            return
        }
        const carts = await Cart.changeCart(type, gid, shopId, orderType)
        const newCategories = categories.upDataCategory(carts.data.car_list)
        this._unLock()
        this.setData({
            carts: carts.data,
            categories: newCategories
        })
    },

    async _initCategories() {
        const shopId = this.data.shop.id
        const orderType = this.data.orderType
        await categories.getAll(this.data.shop.id) // 获取所有数据
        const currentRoot = categories.getCurrentRoot(this.data.current) //获取当前激活的一级分类
        const carts = await Cart.getCart(shopId, orderType) // 获取购物车
        const newCategories = categories.upDataCategory(carts.data.car_list)
        this.setData({
            currentRoot,
            carts: carts.data,
            categories: newCategories,
            current: currentRoot.id
        })
        // 获取二级分类每个分类的高度
        const subsItemHeightArr = categories.getSubsItemHeight((res) => {
            this.data.subsItemHieghtArr = res
        })
    },



    _getShop() {
        const shop = wx.getStorageSync("shopInfo")
        this.setData({
            shop
        })
    },

    _getOrderType() {
        const orderType = wx.getStorageSync("orderType")
        this.setData({
            orderType
        })
    },

    _isLocked() {
        if (this.data.lock) {
            return true
        }
        this.data.lock = true
        return false
    },

    _unLock() {
        this.data.lock = false
    }
})